package com.ctshk.rpc.finance.service.impl;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.rpc.finance.code.AdvanceConstant;
import com.ctshk.rpc.finance.dto.bill.BillGroupRecordExportDTO;
import com.ctshk.rpc.finance.dto.bill.BillGroupRecordSumDTO;
import com.ctshk.rpc.finance.dto.bill.OrderIdDTO;
import com.ctshk.rpc.finance.entity.FinanceBillGroupRecord;
import com.ctshk.rpc.finance.mapper.FinanceBillGroupRecordMapper;
import com.ctshk.rpc.finance.req.bill.BillGroupRecordExportReq;
import com.ctshk.rpc.finance.service.IFinanceBillGroupRecordService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 团期认款记录 服务实现类
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-06-22
 */
@DubboService
public class FinanceBillGroupRecordServiceImpl extends ServiceImpl<FinanceBillGroupRecordMapper, FinanceBillGroupRecord> implements IFinanceBillGroupRecordService {

    @Autowired
    private FinanceBillGroupRecordMapper billGroupRecordMapper;


    /**
     * 导出
     *
     * @param req
     * @return
     */
    @Override
    public List<List<String>> export(BillGroupRecordExportReq req) {
        List<Long> idList = billGroupRecordMapper.queryOrderIdListByDate(req.getDate(),0);
        if (CollectionUtils.isEmpty(idList)){
            throw new BusinessException(SystemError.FINANCE_ERROR_17041);
        }
        List<OrderIdDTO> orderIdList = new ArrayList<>();
        //当前最大序号
        int currentMax = 0;
        for (int i=0;i<idList.size();i++){
            OrderIdDTO orderIdDTO = new OrderIdDTO();
            orderIdDTO.setSaleOrderId(idList.get(i));
            orderIdDTO.setNumber(i+1);
            orderIdList.add(orderIdDTO);
            currentMax ++;
        }
        //返回数据
        List<List<String>> dataList = new ArrayList<>();
        //认款记录明细
        List<BillGroupRecordExportDTO> list = billGroupRecordMapper.queryByDate(req.getDate(),0);
        if (CollectionUtils.isNotEmpty(list)){
            for (BillGroupRecordExportDTO dto : list) {
                List<String> data = new ArrayList<>();
                data.add(AdvanceConstant.COMPONY_CODE);
                data.add(DateUtil.fmtLocalDate(LocalDate.now(),"yyyyMMdd"));
                data.add("1");
                data.add(getOrderIdNumber(dto.getSaleOrderId(),orderIdList));
                data.add(dto.getSummary());
                data.add(dto.getCurrency());
                data.add(dto.getSubectNumber());
                data.add(dto.getType().toString());
                if (Objects.equals(dto.getAdjustType(),2)){
                    //退款
                    data.add("-" + dto.getAmount().toString());
                    data.add("-" + dto.getHkdAmount().toString());
                }else{
                    //收款
                    data.add(dto.getAmount().toString());
                    data.add(dto.getHkdAmount().toString());
                }
                data.add(dto.getExchangeRate().toString());
                data.add(dto.getSaleDeptCode());
                data.add("");
                data.add(dto.getCostCode());
                data.add("");
                data.add(DateUtil.fmtLocalDate(dto.getCreateDate(),"yyyyMMdd"));
                data.add("");
                data.add("");
                data.add("");
                data.add("");
                dataList.add(data);
            }
        }
        //认款记录统计
        List<BillGroupRecordSumDTO> sumList = billGroupRecordMapper.sumByDate(req.getDate(),idList);
        if (CollectionUtils.isNotEmpty(sumList)){
            for (OrderIdDTO orderIdDTO : orderIdList) {
                orderIdDTO.setNumber(currentMax + 1);
                currentMax ++;
            }
            for (BillGroupRecordSumDTO dto : sumList) {
                List<String> data = new ArrayList<>();
                data.add(AdvanceConstant.COMPONY_CODE);
                data.add(DateUtil.fmtLocalDate(LocalDate.now(),"yyyyMMdd"));
                data.add("1");
                data.add(getOrderIdNumber(dto.getSaleOrderId(),orderIdList));
                data.add(dto.getSummary());
                //默认港币
                data.add("HKD");
                data.add(dto.getSubectNumber());
                //赊额方向（0借 1贷）
                data.add("1");
                BigDecimal amount = dto.getCollectionAmount().subtract(dto.getRefundAmount());
                data.add(amount.toString());
                data.add(amount.toString());
                data.add("1");
                data.add(dto.getSaleDeptCode());
                data.add("");
                data.add(dto.getCostCode());
                data.add("");
                data.add(DateUtil.fmtLocalDate(dto.getCreateDate(),"yyyyMMdd"));
                data.add("");
                data.add("");
                data.add("");
                data.add("");
                dataList.add(data);
            }
        }
        return dataList;
    }

    /**
     * 获取臨時憑證行號
     * @param saleOrderId
     * @param orderIdList
     * @return
     */
    private String getOrderIdNumber(Long saleOrderId, List<OrderIdDTO> orderIdList) {
        for (OrderIdDTO dto : orderIdList) {
            if (Objects.equals(dto.getSaleOrderId(),saleOrderId)){
                return dto.getNumber().toString();
            }
        }
        return null;
    }
}
